import 'package:flutter/material.dart';
import 'package:schedule_self_client/ui/pages/calendar_page.dart';
import 'package:schedule_self_client/ui/pages/report_page.dart';
import 'package:schedule_self_client/ui/pages/task_list_page_v2.dart';
import 'package:schedule_self_client/ui/pages/tomato_clock_page.dart';
import 'package:schedule_self_client/ui/pages/user_center_page.dart';
import 'package:schedule_self_client/utils/device_size_util.dart';

class AppLayout extends StatefulWidget {
  const AppLayout({super.key});

  @override
  State<AppLayout> createState() => _AppLayoutState();
}

class _AppLayoutState extends State<AppLayout> {
  late final PageController _pageController;
  int _currentIndex = 0;

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: _currentIndex);
  }

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    DeviceSize deviceSize = DeviceSizeUtil.getSize(context);
    return Scaffold(
      body: SafeArea(
        child: Row(
          children: [
            if (deviceSize != DeviceSize.small) _buildLeftNavigationBar(),
            Expanded(
              child: PageView(
                controller: _pageController,
                physics: const NeverScrollableScrollPhysics(),
                children: const [
                  TaskListPageV2(),
                  CalendarPage(),
                  TomatoClockPage(),
                  ReportPage(),
                  UserCenterPage(),
                ],
                onPageChanged: (index) {
                  setState(() {
                    _currentIndex = index;
                  });
                },
              ),
            ),
          ],
        ),
      ),
      bottomNavigationBar:
          deviceSize == DeviceSize.small ? _buildBottomNavigationBar() : null,
    );
  }

  Widget _buildLeftNavigationBar() {
    return NavigationRail(
      labelType: NavigationRailLabelType.all,
      destinations: const [
        NavigationRailDestination(icon: Icon(Icons.task), label: Text('待办列表')),
        NavigationRailDestination(
            icon: Icon(Icons.calendar_month), label: Text('日历')),
        NavigationRailDestination(icon: Icon(Icons.timer), label: Text('番茄时钟')),
        NavigationRailDestination(
            icon: Icon(Icons.insert_chart), label: Text('报表')),
        NavigationRailDestination(
            icon: Icon(Icons.person), label: Text('用户中心')),
      ],
      selectedIndex: _currentIndex,
      onDestinationSelected: (index) {
        setState(() {
          _currentIndex = index;
          _pageController.jumpToPage(index);
        });
      },
    );
  }

  Widget _buildBottomNavigationBar() {
    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      currentIndex: _currentIndex,
      items: const [
        BottomNavigationBarItem(icon: Icon(Icons.task), label: '待办列表'),
        BottomNavigationBarItem(icon: Icon(Icons.calendar_month), label: '日历'),
        BottomNavigationBarItem(icon: Icon(Icons.timer), label: '番茄时钟'),
        BottomNavigationBarItem(icon: Icon(Icons.insert_chart), label: '报表'),
        BottomNavigationBarItem(icon: Icon(Icons.person), label: '用户中心'),
      ],
      onTap: (index) {
        setState(() {
          _currentIndex = index;
          _pageController.jumpToPage(index);
        });
      },
    );
  }
}
